package abq2.jic.jic.plugin.operate.dailyTask;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.gpt.IGPTAction;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
 *                             _ooOoo_
 *                            o8888888o
 *                            88" . "88
 *                            (| -_- |)
 *                            O\  =  /O
 *                         ____/`---'\____
 *                       .'  \\|     |//  `.
 *                      /  \\|||  :  |||//  \
 *                     /  _||||| -:- |||||-  \
 *                     |   | \\\  -  /// |   |
 *                     | \_|  ''\---/''  |   |
 *                     \  .-\__  `-`  ___/-. /
 *                   ___`. .'  /--.--\  `. . __
 *                ."" '<  `.___\_<|>_/___.'  >'"".
 *               | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *               \  \ `-.   \_ __\ /__ _/   .-` /  /
 *          ======`-.____`-.___\_____/___.-`____.-'======
 *                             `=---='
 *          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *                     佛祖保佑        永无BUG
 *            佛曰:
 *                   写字楼里写字间，写字间里程序员；
 *                   程序人员写程序，又拿程序换酒钱。
 *                   酒醒只在网上坐，酒醉还来网下眠；
 *                   酒醉酒醒日复日，网上网下年复年。
 *                   但愿老死电脑间，不愿鞠躬老板前；
 *                   奔驰宝马贵者趣，公交自行程序员。
 *                   别人笑我忒疯癫，我笑自己命太贱；
 *                   不见满街漂亮妹，哪个归得程序员？
 */
public class GetScheduleInfo implements IGPTAction {
    @Override
    public Map<String, String> invokeAction(String action, Map<String, String> params) {
        Map<String , String> result = new HashMap<>();
        if ("GET_USER_DATA".equalsIgnoreCase(action)) {
            //获取今日时间
            Date todayDate = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String todayDateFormat = simpleDateFormat.format(todayDate);
            DynamicObject[] dynamicObjectArray = BusinessDataServiceHelper.load("abq2_schedule_form", //日程安排的基础资料的实体名
                            "name," +  //任务名
                            "abq2_done_time," + //执行时间
                                    ///单据体部分
                            "abq2_task_context," +  //任务内容
                            "abq2_expect_minute," +  //预期时间
                            "abq2_diff," +  //任务难度
                            "abq2_true_minute," +  //真实花费时间
                            "abq2_finish," +   //完成情况
                            "abq2_task_entryentity"   // 加载单据体
                    ,new QFilter[]{new QFilter("name", QCP.is_notnull, null)});
            //新建日历对象，One代表前一天，Two代表前两天
            Calendar calendarOne = Calendar.getInstance();
            Calendar calendarTwo = Calendar.getInstance();
            calendarOne.setTime(new Date());
            calendarTwo.setTime(new Date());
            calendarOne.add(Calendar.DAY_OF_MONTH , -1);
            calendarTwo.add(Calendar.DAY_OF_MONTH, -2);
            JSONObject dayDataObject = new JSONObject();
            JSONArray jsonArrayTaskList = new JSONArray();
            //循环日任务表单
            for (DynamicObject single : dynamicObjectArray) {
                String objectDate = simpleDateFormat.format(single.getDate("abq2_done_time"));
                //如果日期等于这一天
                if (objectDate.equalsIgnoreCase(todayDateFormat)) {
                    DynamicObjectCollection dynamicObjectCollection = single.getDynamicObjectCollection("abq2_task_entryentity");
                    int sumExpect = 0;
                    int sumFinish = 0;
                    //统计任务时间总数以及日任务数据
                    for (DynamicObject entryObject : dynamicObjectCollection) {
                        JSONArray jsonArray = new JSONArray();
                        String taskName = entryObject.getString("abq2_task_context");
                        jsonArray.add(taskName);
                        jsonArray.add(entryObject.getString("abq2_expect_minute"));
                        jsonArray.add(entryObject.getString("abq2_true_minute"));
                        jsonArrayTaskList.add(jsonArray);
                        sumExpect+=entryObject.getInt("abq2_expect_minute");
                        sumFinish+=entryObject.getInt("abq2_true_minute");
                    }
                    //加入到JSONObject对象
                    getDayObject(1,dayDataObject, objectDate, sumExpect, sumFinish);
                    //若表单日期等于前一天
                } else if (objectDate.equalsIgnoreCase(simpleDateFormat.format(calendarOne.getTime()))) {
                    DynamicObjectCollection dynamicObjectCollection = single.getDynamicObjectCollection("abq2_task_entryentity");
                    //获取时间总数
                    int sumExpect = 0;
                    int sumFinish = 0;
                    for (DynamicObject entryObject : dynamicObjectCollection) {
                        sumExpect+=entryObject.getInt("abq2_expect_minute");
                        sumFinish+=entryObject.getInt("abq2_true_minute");
                    }
                    //加入到JSONObject对象
                    getDayObject(2,dayDataObject, objectDate, sumExpect, sumFinish);
                    //若表单日期等于前两天
                } else if (objectDate.equalsIgnoreCase(simpleDateFormat.format(calendarTwo.getTime()))) {
                    DynamicObjectCollection dynamicObjectCollection = single.getDynamicObjectCollection("abq2_task_entryentity");
                    int sumExpect = 0;
                    int sumFinish = 0;
                    for (DynamicObject entryObject : dynamicObjectCollection) {
                        sumExpect+=entryObject.getInt("abq2_expect_minute");
                        sumFinish+=entryObject.getInt("abq2_true_minute");
                    }
                    getDayObject(3,dayDataObject, objectDate, sumExpect, sumFinish);
                }
            }
            //将获取到的当日任务数据和近三日任务数据放入map
            result.put("statisticsData", jsonArrayTaskList.toJSONString());
            result.put("dayDataObject", dayDataObject.toJSONString());
            System.out.println(result);
        }
        return result;
    }

    private void getDayObject(int day, JSONObject dayDataObject, String objectDate, int sumExpect, int sumFinish) {
        JSONObject dayObject = new JSONObject();
        dayObject.put("day"+day, objectDate);
        dayObject.put("day"+day+"Expect", sumExpect);
        dayObject.put("day"+day+"Finish", sumFinish);
        dayObject.put("day"+day+"IsOvertime", ((sumExpect>sumFinish) ? "否" : "是"));
        dayDataObject.put("day"+day+"Data", dayObject);
    }
}
